package com.quantgroup.data.config;

import com.google.common.base.Stopwatch;
import com.quantgroup.data.metrics.DefaultCollector;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * 请求拦截器
 *
 * @author osmondy
 * @create 2021/9/30 17:05
 */
@Slf4j
public class ValidateInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        StringBuffer requestURL = request.getRequestURL();
//        if (!requestURL.toString().contains("/realTimeData")){
//            return true;
//        }
//
//        // 打印参数
//        Map<String, String[]> parameterMap = request.getParameterMap();
//
//        StringBuffer buffer = new StringBuffer();
//        for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
////            // 对请求参数进行解码
////            if ("ry_adgroup_name".equals(entry.getKey())){
////                String str = "ios%E4%B8%8B%E8%BD%BD-%E6%8A%96%E9%9F%B3-%E8%A7%86%E9%A2%91-1007-%E8%87%AA%E5%8A%A8";
////                String[] split = StrUtil.split(str, str.length());
////                parameterMap.put("ry_adgroup_name", split);
////            }
////            if ("ry_adplan_name".equals(entry.getKey())){
////                String str = "%E6%8A%96%E9%9F%B3-%E5%89%AA%E8%BE%91-%E8%A3%85%E4%BF%AE%E5%A5%BD%E4%BA%86-%E8%87%AA%E5%8A%A8%E5%87%BA%E4%BB%B7-1007-%E6%A0%87%E7%AD%BE2";
////                String[] split = StrUtil.split(str, str.length());
////                parameterMap.put("ry_adplan_name", split);
////            }
////            if ("ry_adcreative_name".equals(entry.getKey())){
////                parameterMap.put("ry_adcreative_name", null);
////            }
//            buffer.append("," + entry.getKey() + "=" + Arrays.toString(entry.getValue()));
//        }
//        log.warn("【参数】：" + buffer.toString().substring(1));
//        // 校验skey
//        // 1、获取参数 activetime、appkey,校验appkey是否正确。
//        String activetime = request.getParameter("activetime");
//        String appkey = request.getParameter("appkey");
//        String skey = request.getParameter("skey");
//        AppKeyEnum appKeyEnum = AppKeyEnum.getAppKey(appkey);
//        if (appKeyEnum == null) {
//            throw new BusinessException("appkey请求参数错误");
//        }
//        // 2、拼接 activetime+appkey(大写)+securitykey
//        String appendStr = activetime + "_" + appkey.toUpperCase() + "_" + appKeyEnum.getSecurity();
//
//        // 3、字符串MD5并转大写。
//        String mySkey = SecureUtil.md5(appendStr).toUpperCase();
//        if (!skey.equals(mySkey)) {
//            throw new BusinessException("skey校验失败");
//        }
        // 4、对参数进行处理
//        request.setAttribute("ry_adgroup_name", "ios%E4%B8%8B%E8%BD%BD-%E6%8A%96%E9%9F%B3-%E8%A7%86%E9%A2%91-1007-%E8%87%AA%E5%8A%A8");
//        request.setAttribute("ry_adplan_name", "%E6%8A%96%E9%9F%B3-%E5%89%AA%E8%BE%91-%E8%A3%85%E4%BF%AE%E5%A5%BD%E4%BA%86-%E8%87%AA%E5%8A%A8%E5%87%BA%E4%BB%B7-1007-%E6%A0%87%E7%AD%BE2");
//        request.setAttribute("ry_adcreative_name", null);
//
//        String ryAdgroupName = request.getParameter("ry_adgroup_name");
//        if (StrUtil.isNotEmpty(ryAdgroupName)){
//            String decode = URLDecoder.decode(ryAdgroupName, "UTF-8");
//            request.setAttribute("ry_adgroup_name", decode);
//        }
//        String ryAdplanName = request.getParameter("ry_adplan_name");
//        if (StrUtil.isNotEmpty(ryAdplanName)){
//            String decode = URLDecoder.decode(ryAdplanName, "UTF-8");
//            request.setAttribute("ry_adplan_name", decode);
//        }
//        String ryAdcreativeName = request.getParameter("ry_adcreative_name");
//        if (StrUtil.isNotEmpty(ryAdcreativeName)){
//            String decode = URLDecoder.decode(ryAdcreativeName, "UTF-8");
//            request.setAttribute("ry_adcreative_name", decode);
//        }
//
//        StringBuffer buffer2 = new StringBuffer();
//        for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
//            buffer2.append("," + entry.getKey() + "=" + Arrays.toString(entry.getValue()));
//        }
//        log.warn("【修改后参数】：" + buffer2.toString().substring(1));

        Stopwatch stopwatch = Stopwatch.createStarted();

        Stopwatch stop = stopwatch.stop();
        long time = stop.elapsed(TimeUnit.MILLISECONDS);

        DefaultCollector.requestSpendMetrics(requestURL.toString(), time);
        DefaultCollector.requestMetrics(requestURL.toString());

//        metricsCollector.requestMetrics(requestURL.toString());


        return true;
    }
}
